Imports ComponentGo.Calendars
Imports Phoenix.DataAccess
Imports Phoenix.Events

Namespace Forms.Area

    Public Class MonthCalendarArea
        Inherits CalendarAreaBase

#Region " Windows Form Designer generated code "

        Public Sub New()
            MyBase.New()

            'This call is required by the Windows Form Designer.
            InitializeComponent()

            'Add any initialization after the InitializeComponent() call
            Calendar = MonthlyCalendar
        End Sub

        'UserControl overrides dispose to clean up the component list.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub

        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer

        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        Friend WithEvents MonthlyCalendar As ComponentGo.Calendars.MonthlyCalendar
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            Me.MonthlyCalendar = New ComponentGo.Calendars.MonthlyCalendar
            Me.SuspendLayout()
            '
            'MonthlyCalendar
            '
            Me.MonthlyCalendar.AllowOverlap = False
            Me.MonthlyCalendar.DayLineColor = System.Drawing.Color.FromArgb(CType(215, Byte), CType(177, Byte), CType(0, Byte))
            Me.MonthlyCalendar.Dock = System.Windows.Forms.DockStyle.Fill
            Me.MonthlyCalendar.EnableEditInPlace = False
            Me.MonthlyCalendar.ExtraBreakHourColor = System.Drawing.Color.Fuchsia
            Me.MonthlyCalendar.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(204, Byte))
            Me.MonthlyCalendar.FreeDay = ComponentGo.Calendars.FreeDays.None
            Me.MonthlyCalendar.FreeHourColor = System.Drawing.Color.FromArgb(CType(255, Byte), CType(238, Byte), CType(158, Byte))
            Me.MonthlyCalendar.Location = New System.Drawing.Point(0, 0)
            Me.MonthlyCalendar.Name = "MonthlyCalendar"
            Me.MonthlyCalendar.Size = New System.Drawing.Size(360, 448)
            Me.MonthlyCalendar.TabIndex = 0
            '
            'MonthCalendarArea
            '
            Me.Controls.Add(Me.MonthlyCalendar)
            Me.Name = "MonthCalendarArea"
            Me.ResumeLayout(False)

        End Sub

#End Region

#Region " Area Behaviour "

        Public Overrides ReadOnly Property HasResources() As Boolean
            Get
                Return False
            End Get
        End Property
#End Region

#Region " Time Navigation "
        Public Overrides Function SetNext() As Date
            MonthlyCalendar.FirstDate = MonthlyCalendar.FirstDate.AddMonths(1)
            Return MonthlyCalendar.FirstDate
        End Function
        Public Overrides Function SetPrev() As Date
            MonthlyCalendar.FirstDate = MonthlyCalendar.FirstDate.AddMonths(-1)
            Return MonthlyCalendar.FirstDate
        End Function
        Public Overrides Function SetToday() As Date
            MonthlyCalendar.FirstDate = Date.Today
            Return MonthlyCalendar.FirstDate
        End Function
        Public Overrides Function SetDate(ByVal d As Date) As Date
            MonthlyCalendar.FirstDate = d
            Return MonthlyCalendar.FirstDate
        End Function

#End Region

#Region " Event Handlers "
        Private Sub MonthlyCalendar_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MonthlyCalendar.MouseDown
            If e.Clicks < 2 Then Return
            If e.Button <> MouseButtons.Left Then Return
            If Not MonthlyCalendar.Appointments.FirstSelected Is Nothing Then Return

            If MonthlyCalendar.HitTest(New Point(e.X, e.Y)) = TabularCalendar.Regions.DaysWorkspace Then
                AppRaiseEvent(Me, New PhoenixCalendarEventArgs(PhoenixCalendar.DayCalendar, MonthlyCalendar.SelectedDateBegin))
            End If
        End Sub

        Private Sub MonthlyCalendar_FirstDateChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MonthlyCalendar.FirstDateChanged
            OnFirstDateChanged(New DateRangeEventArgs(FirstDate, FirstDate))
        End Sub
#End Region

        Public Overrides Property FirstDate() As Date
            Get
                Return MonthlyCalendar.FirstDate
            End Get
            Set(ByVal Value As Date)
                MonthlyCalendar.FirstDate = Value
            End Set
        End Property

        Public Overrides Sub Paste(ByVal arr As IList)
            If arr Is Nothing Then
                Return
            End If
            For Each obj As DataRow In arr
                Dim item As DataAccess.DataSetAppointments.Appointment = Helpers.AppointmentHelper.Copy(obj)
                item.StartTime = Calendar.SelectedDateBegin.Add(item.StartTime.TimeOfDay)
                item.FinishTime = item.StartTime.AddMinutes(item.ExpectedDuration)
                If Helpers.AppointmentHelper.IsValidTime(item.StartTime, item.FinishTime, item) Then
                    Helpers.AppointmentHelper.UpdateAppointment(item, Me)
                Else
                    AppointmentForm.OpenForm(item)
                End If
            Next
        End Sub

    End Class
End Namespace

